﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Prod;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmPrintFormWorkingOrder : BaseForm.frmBaseDialogyForm
    {
        #region vars
        private ProdPlanOrderItemEntity SelectedProdPlanOrderItem;
        private DataTable DtProdProductTechnic = new DataTable();
        private DataTable DtProdProductTechnicItem = new DataTable();
        private DataSet Ds = new DataSet();
        #endregion

        #region ctors
        public frmPrintFormWorkingOrder(ProdPlanOrderItemEntity entity)
        {
            InitializeComponent();
            SelectedProdPlanOrderItem = entity;
        }
        #endregion

        #region functions
        private void BindGridData()
        {
            if (SelectedProdPlanOrderItem == null)
                return;

            string Condition = " AND PLAN_ORDER_ITEM_ID='" + SelectedProdPlanOrderItem.PlanOrderItemId + "' ";
            DataTable Dt = ProdProductTechnicBs.GetDtProdFlowByCondition(Condition, " SEQUENCE");
            DtProdProductTechnic = Dt.Copy();

            DtProdProductTechnicItem.Columns.Add("PRODUCT_TECHNIC_ITEM_ID", typeof(string));
            DtProdProductTechnicItem.Columns.Add("PRODUCT_TECHNIC_ID", typeof(string));
            DtProdProductTechnicItem.Columns.Add("GROUP_PERSON", typeof(string));
            DtProdProductTechnicItem.Columns.Add("GROUP_PERSON_NAME", typeof(string));
            DtProdProductTechnicItem.Columns.Add("QTY", typeof(decimal));
            DtProdProductTechnicItem.Columns.Add("PLAN_STARTTIME", typeof(DateTime));
            DtProdProductTechnicItem.Columns.Add("PLAN_ENDTIME", typeof(DateTime));
            DtProdProductTechnicItem.Columns.Add("WORKING_TIME", typeof(decimal));
            DtProdProductTechnicItem.Columns.Add("TIME_COEFFICIENT", typeof(decimal));

            DtProdProductTechnic.TableName = "PROD_PRODUCT_TECHNIC";
            DtProdProductTechnicItem.TableName = "PROD_PRODUCT_TECHNIC_ITEM";
            Ds.Tables.Add(DtProdProductTechnic);
            Ds.Tables.Add(DtProdProductTechnicItem);

            DataColumn KeyColumn = Ds.Tables["PROD_PRODUCT_TECHNIC"].Columns["PRODUCT_TECHNIC_ID"];         //主键
            DataColumn ForeignColumn = Ds.Tables["PROD_PRODUCT_TECHNIC_ITEM"].Columns["PRODUCT_TECHNIC_ID"];    //外键
            Ds.Relations.Add("明细", KeyColumn, ForeignColumn);     //从表的层次名

            foreach (DataRow dr in DtProdProductTechnic.Rows)
            {
                DataTable dtDetail = ProdProductTechnicItemBs.GetDtEntityByProductTechnicID(dr["PRODUCT_TECHNIC_ID"].ToString());
                if (dtDetail != null && dtDetail.Rows.Count > 0)
                {
                    foreach (DataRow drDetail in dtDetail.Rows)
                    {
                        Ds.Tables["PROD_PRODUCT_TECHNIC_ITEM"].ImportRow(drDetail);
                    }
                }
            }

        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            if (SelectedProdPlanOrderItem != null)
            {
                BindGridData();
                if (Ds.Tables[0].Rows.Count > 0)
                {
                    frmPrintTemplateWorkingOrder fXtraReport = new frmPrintTemplateWorkingOrder(SelectedProdPlanOrderItem, Ds);
                    printControl1.PrintingSystem = fXtraReport.PrintingSystem;
                    fXtraReport.CreateDocument();
                }
            }
        }
        #endregion
    }
}
